import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.HashSet;

class parseData
{
    public class Route {
	String route_id;
	String route_color;
	HashSet shape_id;
	public Route()
	{
	    shape_id = new HashSet();
	}
    }
    private HashMap routeMap = new HashMap();
    private HashMap hashMap = new HashMap();
    private Route routes[];
    private int routes_nbr;
    private BufferedReader br;
    public void getIOStream(String filename)
    {
	try {
	    FileInputStream fstream = new FileInputStream(filename);
	    DataInputStream in = new DataInputStream(fstream);
	    br = new BufferedReader(new InputStreamReader(in));
	} catch (Exception e) {
	    System.out.println("Error 1: " + e.getMessage());
	}
    }

    public void fillinRoutes()
    {
	try {
	    String line;
	    while ((line = br.readLine()) != null) {
		String splt[] = line.split(",");
		routes[routes_nbr] = new Route();
		routes[routes_nbr].route_id = new String(splt[0]);
		routes[routes_nbr].route_color = new String(splt[5]);
		hashMap.put(new String(splt[0]), new Integer(routes_nbr));
		++routes_nbr;
	    }
	    br.close();
	} catch (Exception e) {
	    System.out.println("Error 2: " + e.getMessage());
	}
    }

    public void fillinTrips()
    {
	try {
	    String line;
	    int linno = 0;
	    while ((line = br.readLine()) != null) {
		//System.out.println("line " + line);
		String splt[] = line.split(",");
		if (hashMap.containsKey(new String(splt[0]))) {
		    Integer route_id = (Integer)hashMap.get(new String(splt[0]));
		    routes[route_id.intValue()].shape_id.add(Integer.parseInt(splt[5]));
		    routeMap.put(Integer.parseInt(splt[5]), route_id.intValue());
		    //System.out.println(linno + " " + route_id.intValue() + " " + splt[5]);
		    //++linno;
		}
	    }
	    br.close();
	} catch (Exception e) {
	    System.out.println("Error 3: " + e.toString());
	}
    }

    public void fillinShapes()
    {
	try {
	    FileWriter fstream = new FileWriter("out.txt");
	    BufferedWriter out = new BufferedWriter(fstream);
	    String line;
	    while ((line = br.readLine()) != null) {
		String splt[] = line.split(",");
		if (routeMap.containsKey(Integer.parseInt(splt[0]))) {
		    //out.write(line + ",");
		    int route_id = ((Integer)routeMap.get(Integer.parseInt(splt[0]))).intValue();
		    out.write(line + "," + routes[route_id].route_id + "," + routes[route_id].route_color+"\n");
		    System.out.println(line+","+routes[route_id].route_id+","+routes[route_id].route_color);
		}
	    }
	    out.close();
	    br.close();
	} catch (Exception e) {
	    System.out.println("Error 5: " + e.toString());
	}
    }

    public void splitFile()
    {
	int i = 0;
	getIOStream("out.txt");
	try {
	    FileWriter fstream[] = new FileWriter[30];
	    BufferedWriter out[] = new BufferedWriter[30];
	    String line;
	    int linno = 0;
	    line = br.readLine();
	    String splt[] = line.split(",");
	    String marker = new String(splt[0]);
	    String colors[] = new String[30];

	    fstream[i] = new FileWriter("out" + i + ".txt");
	    out[i] = new BufferedWriter(fstream[i]);
	    out[i].write(line + "\n");
	    colors[i] = new String(splt[splt.length-1]);
	    System.out.println("color " + colors[i]);
	    while ((line = br.readLine()) != null) {
		splt = line.split(",");
		if (splt[0].equals(marker))
		    out[i].write(line + "\n");
		else {
		    out[i].flush();
		    ++i;
		    marker = new String(splt[0]);
		    fstream[i] = new FileWriter("out" + i + ".txt");
		    out[i] = new BufferedWriter(fstream[i]);
		    out[i].write(line + "\n");
		    colors[i] = new String(splt[splt.length-1]);
		    System.out.println("color " + colors[i]);
		}
	    }
	    System.out.println("writing colors");
	    FileWriter fstreamc = new FileWriter("colors.txt");
	    BufferedWriter outc = new BufferedWriter(fstreamc);
	    for (i=0; i<30; ++i) {
		outc.write("#" + colors[i] + ", ");
	    }
	    outc.flush();
	} catch (Exception e) {
	    System.out.println("Error 6: " + e.toString());
	}
    }

    public parseData()
    {
	routes = new Route[10];
	routes_nbr = 0;
        try {
            getIOStream("routes.txt");
            fillinRoutes();
            for (int i=0; i<routes_nbr; ++i) {
                System.out.println(routes[i].route_id + " " + routes[i].route_color);
            }

	    getIOStream("trips.txt");
	    fillinTrips();
	    for (int i=0; i<routes_nbr; ++i) {
		System.out.println(routes[i].route_id);
		Iterator it = routes[i].shape_id.iterator();
		while (it.hasNext()) {
		    Integer elem = (Integer)it.next();
		    System.out.print(" " + elem);
		}
		System.out.println();
	    }
	    Iterator it = routeMap.keySet().iterator();
	    while (it.hasNext()) {
		int key = ((Integer)it.next()).intValue();
		System.out.println(key + " " + routeMap.get(key));
	    }

	    getIOStream("shapes.txt");
	    fillinShapes();

	    splitFile();
        } catch (Exception e) {//Catch exception if any                                            
            System.err.println("Error 4: " + e.getMessage());
        }
    }

    public static void main(String args[])
    {
	parseData ps = new parseData();
    }
}